﻿Imports Microsoft.VisualBasic

Namespace WebApi
    Namespace Google
        Public Class GoogleClient

            Private webClient As WebApi.Net.WebClient

            Public Sub New(oauth2 As WebApi.Oauth.Oauth2)

                Me.webClient = New WebApi.Net.WebClient
                Me.webClient.oAuth2 = oauth2

            End Sub

            Public Property LastError As Net.ServiceError
            Private Function Download(address As String) As String

                Dim Res = Encoding.UTF8.GetString(Me.webClient.DownloadData(address))

                webClient.postParams.Clear()

                If Me.webClient.StatusCode <> 200 Then

                    Me.LastError = New Net.ServiceError With {
                        .webClient = Me.webClient,
                       .ResError = Res,
                       .Net = WebApi.Type.GOOGLE
                   }

                    Select Case Me.webClient.StatusCode
                        Case 500, 502, 503, 504
                            'Internal Server Error
                            'Bad Gateway
                            'Service Unavailable
                            'Gateway timeout

                            'Me.LastError = New Net.ServiceError With {.Message = Me.webClient.StatusCodeDescr, .Code = Me.webClient.StatusCode}
                            Me.LastError.Code = Me.webClient.StatusCode
                            Me.LastError.Message = Me.webClient.StatusCodeDescr

                            Net.ExceptionBase.ThrowException(New Net.ExceptionConnectionServer With {.ServiceError = Me.LastError})

                        Case Else

                            Try
                                'Dim Err As ServiceErrors
                                'Err = MyApp.JSONDeserialize(Of ServiceErrors)(Res)

                                'Me.LastError.Code = Err.error.code
                                'Me.LastError.Message = Err.error.message

                            Catch ex As Exception

                                Me.LastError.Code = Me.webClient.StatusCode
                                Me.LastError.Message = Res

                            End Try


                            Select Case Me.webClient.StatusCode

                                Case 400
                                    'Unauthorized
                                    Select Case Me.LastError.Code
                                        Case 190
                                            '190 not authorized application
                                            Net.ExceptionBase.ThrowException(New Net.ExceptionAPIAccount With {.ServiceError = Me.LastError})

                                    End Select

                            End Select

                    End Select

                    Net.ExceptionBase.ThrowException(New Net.ExceptionBase With {.ServiceError = Me.LastError})

                End If

                Return Res

            End Function
            Public Function GetPeople(id As String) As People

                Dim Uri As New UriBuilder(SF("https://www.googleapis.com/plus/v1/people/{0}", id))

                Dim queryString = HttpUtility.ParseQueryString("")

                'queryString.Add("format", "json")

                Uri.Query = queryString.ToString

                Dim Res = Me.Download(Uri.Uri.AbsoluteUri)
                'Return Res
                Return MyApp.JSONDeserializeObj(Of People)(Res)


            End Function

            Public Function GetPeopleList(id As String, pageToken As String) As PeopleFeed

                Dim Uri As New UriBuilder(SF("https://www.googleapis.com/plus/v1/people/{0}/people/visible", id))

                Dim queryString = HttpUtility.ParseQueryString("")

                If pageToken <> "" Then
                    queryString.Add("pageToken", pageToken)
                End If
                queryString.Add("maxResults", 100)

                Uri.Query = queryString.ToString

                Dim Res = Me.Download(Uri.Uri.AbsoluteUri)
                'Return Res
                Return MyApp.JSONDeserializeObj(Of PeopleFeed)(Res)


            End Function

            Public Function GetactivitiesSearch(q As String) As activityFeed

                Dim Uri As New UriBuilder(SF("https://www.googleapis.com/plus/v1/activities"))

                Dim queryString = HttpUtility.ParseQueryString("")

                queryString.Add("query", q)
                queryString.Add("orderBy", "recent")
                queryString.Add("maxResults", 20)

                'If since <> "" Then
                '    queryString.Add("since", since)
                'End If
                'If until <> "" Then
                '    queryString.Add("until", until)
                'End If

                Uri.Query = queryString.ToString


                Dim Res = Me.Download(Uri.Uri.AbsoluteUri)

                'Dim o = MyApp.JSONDeserialize(Res)

                'Return o("data")

                Return MyApp.JSONDeserializeObj(Of activityFeed)(Res)


            End Function

            Public Function PostMoment(id As String) As ItemScope

                Dim Uri As New UriBuilder(SF("https://www.googleapis.com/plus/v1/people/{0}/moments/vault", id))

                Dim queryString = HttpUtility.ParseQueryString("")

                'queryString.Add("debug", "true")
                queryString.Add("request_visible_actions", Net.WebClient.UrlEncode("http://schemas.google.com/AddActivity http://schemas.google.com/ReviewActivity"))

                Dim m = New Moment With {
                    .type = "http://schemas.google.com/AddActivity",
                    .target = New ItemScope With {
                        .description = "A page that describes just how awesome Google+ is!",
                        .type = "http://schemas.google.com/AddActivity", .image = "https://developers.google.com/+/plugins/snippet/examples/thing.png",
                        .name = "The Google+ Platform", .id = "34r43rfef43f"
                        }
                }

                webClient.postParams.Add("*", MyApp.JSONSerialize(m))

                Uri.Query = queryString.ToString

                Dim Res = Me.Download(Uri.Uri.AbsoluteUri)
                'Return Res
                Return MyApp.JSONDeserializeObj(Of ItemScope)(Res)


            End Function

        End Class

        Public Class People

            Public Property id As String
            Public Property kind As String
            Public Property gender As String
            Public Property emails As New List(Of Email)
            Public Property displayName As String

            Public Property url As String
            Public Property image As Image

            Public Property placesLived As New List(Of place)

            Public Property language As String


        End Class

        Public Class Email
            Public Property value As String
            Public Property type As String

        End Class

        Public Class Image
            Public Property url As String
        End Class

        Public Class Place
            Public Property value As String
        End Class

        Public Class Moment
            Public Property kind As String = "plus##moment"
            Public Property target As ItemScope
            Public Property type As String
        End Class

        Public Class ItemScope
            Public Property kind As String = "plus##itemScope"
            Public Property id As String
            Public Property type As String
            Public Property image As String
            Public Property description As String
            Public Property name As String

        End Class

        Public Class PaginationFeed(Of tres)
            Public Property kind As String
            Public Property nextPageToken As String
            Public Property title As String
            Public Property items As New List(Of tres)

        End Class

        '"plus#activityFeed"
        Public Class ActivityFeed
            Inherits PaginationFeed(Of activity)


        End Class
        Public Class PeopleFeed
            Inherits PaginationFeed(Of People)

        End Class

        Public Class activity
            Public Property kind As String = "plus#activity"
            Public Property published As String
            Public Property id As String
            Public Property verb As String
            Public Property annotation As String
            Public Property [object] As [object]
            Public Property actor As actor

        End Class
        Public Class [object]
            Public Property content As String
            Public Property attachments As New List(Of attachments)

        End Class
        Public Class attachments
            Public Property objectType As String
            Public Property displayName As String
            Public Property image As Image

        End Class



        Public Class actor
            Public Property id As String
            Public Property displayName As String

        End Class

    End Namespace
End Namespace

